package com.jingdianjichi.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.jingdianjichi.subject.common.enums.isDeleteFlagEnum;
import com.jingdianjichi.subject.domain.convent.SubjectCategoryConverter;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.entity.SubjectLabel;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 题目分类领域服务
 */
@Service
@Slf4j
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {

    @Resource
    private SubjectCategoryService subjectCategoryService;

    @Resource
    private SubjectMappingService subjectMappingService;
    @Resource
    private SubjectLabelService subjectLabelService;

    /**
     * 新增分类
     *
     * @param subjectCategoryBO 分类BO对象
     */
    @Override
    public void add(SubjectCategoryBO subjectCategoryBO) {
        // TODO:参数校验
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryDomainServiceImpl.add.dto:{}", JSON.toJSONString(subjectCategoryBO));
        }
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE
                .converBoToCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(isDeleteFlagEnum.UN_DELETED.getCode());
        subjectCategoryService.insert(subjectCategory);
    }

    /**
     * 查询岗位大类 一级分类
     *
     * @param subjectCategoryBO BO对象 用于查询条件
     * @return List<SubjectCategoryBO> 一级分类集合数据
     */
    @Override
    public List<SubjectCategoryBO> queryCategory(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.converBoToCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(isDeleteFlagEnum.UN_DELETED.getCode());
        // 调用Service查询一级分类
        List<SubjectCategory> subjectCategoryList = subjectCategoryService.queryCategory(subjectCategory);
        // 将查询结果转换为BO对象
        List<SubjectCategoryBO> boList = SubjectCategoryConverter.INSTANCE
                .converBoToCategory(subjectCategoryList);
        // TODO:日志参数校验
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryDomainServiceImpl.queryPrimaryCategory.boList:{}", JSON.toJSONString(boList));
        }
        boList.forEach(bo -> {
            Integer subjectCount = subjectCategoryService.querySubjectCount(bo.getId());
            bo.setCount(subjectCount);
        });
        return boList;
    }

    /**
     * 更新分类
     *
     * @param subjectCategoryBO 查询条件
     * @return 是否成功
     */
    @Override
    public Boolean update(SubjectCategoryBO subjectCategoryBO) {
        // TODO:日志参数校验
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryDomainServiceImpl.update.bo:{}", JSON.toJSONString(subjectCategoryBO));
        }
        int count = subjectCategoryService.update(SubjectCategoryConverter.INSTANCE.converBoToCategory(subjectCategoryBO));
        return count > 0;
    }

    /**
     * 删除分类
     *
     * @param subjectCategoryBO 查询条件
     * @return 是否成功
     */
    @Override
    public Boolean delete(SubjectCategoryBO subjectCategoryBO) {
        // TODO:日志参数校验
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryDomainServiceImpl.delete.bo:{}", JSON.toJSONString(subjectCategoryBO));
        }
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.converBoToCategory(subjectCategoryBO);
        // 设置删除标识
        subjectCategory.setIsDeleted(isDeleteFlagEnum.DELETED.getCode());
        return subjectCategoryService.update(subjectCategory) > 0;
    }

    @Override
    public List<SubjectCategoryBO> queryCategoryAndLabel(SubjectCategoryBO subjectCategoryBO) {
        // 查询当前大类下所有分类，
        SubjectCategory subjectCategory = new SubjectCategory();
        subjectCategory.setParentId(subjectCategory.getId());
        subjectCategory.setIsDeleted(isDeleteFlagEnum.UN_DELETED.getCode());
        List<SubjectCategory> subjectCategoryList = subjectCategoryService.queryCategory(subjectCategory);
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryDomainServiceImpl.queryCategoryAndLabel.categoryList:{}",
                    JSON.toJSONString(subjectCategoryList));
        }
        List<SubjectCategoryBO> categoryBOList = SubjectCategoryConverter.INSTANCE.converBoToCategory(subjectCategoryList);
        //一次获取标签信息
        categoryBOList.forEach(category -> {
            SubjectMapping subjectMapping = new SubjectMapping();
            subjectMapping.setCategoryId(category.getId());
            List<SubjectMapping> mappingList = subjectMappingService.queryLableId(subjectMapping);
            if (CollectionUtils.isEmpty(mappingList)) {
                return;
            }
            List<Long> lableIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
            List<SubjectLabel> labelList = subjectLabelService.batchQueryLabelById(lableIdList);
            List<SubjectLabelBO> labelBOList = new LinkedList<>();
            labelList.forEach(label -> {
                SubjectLabelBO subjectLabelBO = new SubjectLabelBO();
                subjectLabelBO.setId(label.getId());
                subjectLabelBO.setLabelName(label.getLabelName());
                subjectLabelBO.setCategoryId(label.getCategoryId());
                subjectLabelBO.setSortNum(label.getSortNum());
                labelBOList.add(subjectLabelBO);
            });
            category.setSubjectLabelBOList(labelBOList);

        });
        return categoryBOList;
    }
}
